﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Editor.aspx.cs" Inherits="Test_Editor" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../js/jquery.js" type="text/javascript"></script>
    <link type="text/css" rel="Stylesheet" href="/css/Dialog.css" />
    <script src="/js/Editor.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <%--<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />--%>
        <telerik:RadEditor Width="735" Height="500" ID="radEditorContent" ToolsFile="~/App_Data/Xml/DefaultToolbar.xml"
            runat="server" Skin="Default" EditModes="All" NewLineBr="True" EnableEmbeddedScripts="true"
            StripFormattingOptions="Css,Font,Span,MSWord,MSWordNoFonts,MSWordRemoveAll" StripFormattingOnPaste="Css,Font,Span,MSWord,MSWordNoFonts,MSWordRemoveAll"
            ToolbarMode="Default" SpellCheckSettings-AllowAddCustom="false" EnableResize="false"
            OnClientCommandExecuted="OnClientCommandExecuted" OnClientPasteHtml="OnClientPasteHtml1"
            ContentFilters="DefaultFilters">
            <CssFiles>
                <telerik:EditorCssFile Value="/css/Editor.css" />
            </CssFiles>
        </telerik:RadEditor>
        <script language="javascript" type="text/javascript">
            var regexWhiteSpace = new RegExp("<(p[^>]*)>(((\&nbsp;)*\s*)*)?</p>|<(p[^>]*)><(span[^>]*)>(((\&nbsp;)*\s*)*)?</span></p>|<(font[^>]*)>|<(/font[^>]*)>|<o:p>|</o:p>|<(st1[^>]*)>|<(/st1[^>]*)>|&nbsp;", "gi");
            var regexP = new RegExp("(<p|<P)[^>]*>(.*?)(<\/P>|<\/p>)", "gi");
            var regexOpenDIV = new RegExp("(<div|<DIV)[^>]*>", "gi");
            var regexCloseDIV = new RegExp("(<\/DIV>|<\/div>)", "gi");
            var regexSpan = new RegExp("(<span[^*>]*>)|(<\/span>)|(<SPAN[^*>]*>)|(<\/SPAN>)", "gi");
            /*var regexFont = new RegExp("(<font[^*>]*>)|(<\/font>)|(<FONT[^*>]*>)|(<\/FONT>)", "gi");*/
            var regexFont = new RegExp("(<font|<FONT)[^>]*>(.*?)(<\/FONT>|<\/font>)", "gi");
            var regexStrong = new RegExp("(<strong[^*>]*>)|(<\/strong>)|(<STRONG[^*>]*>)|(<\/STRONG>)", "gi");
            var regexB = new RegExp("(<b|<B)[^>]*>(.*?)(<\/B>|<\/b>)", "gi");
            var regexU = new RegExp("(<u|<U)[^>]*>(.*?)(<\/U>|<\/u>)", "gi");
            var regexI = new RegExp("(<i|<I)[^>]*>(.*?)(<\/I>|<\/i>)", "gi");
            var regexTABLE = new RegExp("(<table|<TABLE)[^>]*>(.*?)(<\/TABLE>|<\/table>)", "gi");
            var regexH1 = new RegExp("(<h1|<H1)[^>]*>(.*?)(<\/H1>|<\/h1>)", "gi");
            var regexH2 = new RegExp("(<h2|<H2)[^>]*>(.*?)(<\/H2>|<\/h2>)", "gi");
            var regexEM = new RegExp("(<em|<EM)[^>]*>(.*?)(<\/EM>|<\/em>)", "gi");
            var regexA = new RegExp("(<a|<A)[^>]*>(.*?)(<\/A>|<\/a>)", "gi");
            var regexUL = new RegExp("(<ul|<UL)[^>]*>(.*?)(<\/UL>|<\/ul>)", "gi");

            function OnClientPasteHtml1(editor, args) {
                var commandName = args.get_commandName();
                var value = args.get_value();

                if (commandName == "Paste") {

                    var pasteContent = value;
                    pasteContent = pasteContent.replace(regexOpenDIV, "<P>");
                    pasteContent = pasteContent.replace(regexCloseDIV, "</P>");
                    pasteContent = pasteContent.replace(regexP, "<P>$2</P>").replace(regexWhiteSpace, " ");
                    pasteContent = pasteContent.replace(regexSpan, "");
                    /*pasteContent = pasteContent.replace(regexFont, "");*/
                    pasteContent = pasteContent.replace(regexFont, "$2");
                    pasteContent = pasteContent.replace(regexStrong, "");
                    pasteContent = pasteContent.replace(regexB, "$2");
                    pasteContent = pasteContent.replace(regexU, "$2");
                    pasteContent = pasteContent.replace(regexI, "$2");
                    pasteContent = pasteContent.replace(regexTABLE, "");
                    pasteContent = pasteContent.replace(regexH1, "<P>$2</P>");
                    pasteContent = pasteContent.replace(regexH2, "<P>$2</P>");
                    pasteContent = pasteContent.replace(regexEM, "$2");
                    pasteContent = pasteContent.replace(regexA, "$2");

                    pasteContent = pasteContent.replace(/size|SIZE = ([\d]{1})/g, '');

                    // plain text
                    //if (!pasteContent.match(/(<([^>]+)>)/gi))
                    pasteContent = "<P>" + pasteContent + "</P>";

                    //set the modified pasted content in the editor
                    args.set_value(pasteContent);
                }
            }

            function OnClientCommandExecuted(editor, args) {
                if ("ApplyClass" == args.get_commandName()) {
                    var content = editor.get_html(true);
                    content = content.replace(/span/gi, "p");
                    editor.set_html(content);
                }
            }


            // Insert Image From List Into Editor Content
            Telerik.Web.UI.Editor.CommandList["CustomImageManager"] = function (commandName, radEditorContent, args) {
                var insertImageCallbackFunction = function (sender, args) {
                    if (!args.IsCancel) {
                        var html = GetImageAttribute(args);
                        radEditorContent.pasteHtml(html);
                    }
                };
                // Indent
                Telerik.Web.UI.Editor.CommandList["CustomerIndent"] = function (commandName, editor, args) {
                    editor.pasteHtml('<span style="width:200px;border: 1px dashed #bb0000;background-color: #fafafa;color: blue;"> ' + args.get_value() + ' </span>');
                };

                radEditorContent.showExternalDialog(
                '' + '/Admin/Modules/FileManager/InsertFTPImage.aspx', {},
                800, 620, insertImageCallbackFunction, null, null, true,
                Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move, false, true);
            };

            // Insert Document From List Into Editor Content
            Telerik.Web.UI.Editor.CommandList["CustomDocumentManager"] = function (commandName, radEditorContent, args) {

                var argument = radEditorContent.getSelectionHtml();
                var insertDocumentCallbackFunction = function (sender, args) {
                    if (!args.IsCancel) {
                        var html = GetDocumentAttribute(args);
                        radEditorContent.pasteHtml(html);
                    }
                };

                radEditorContent.showExternalDialog(
            '' + '/Admin/Modules/FileManager/InsertFTPDocument.aspx', argument,
            780, 550, insertDocumentCallbackFunction, null, null, true,
            Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move, false, true);
            };

            // Flash Video
            Telerik.Web.UI.Editor.CommandList["CustomVideoManager"] = function (commandName, radEditorContent, args) {
                var insertVideoCallbackFunction = function (sender, args) {
                    if (!args.IsCancel) {
                        var html = GetVideoAttribute(args);
                        radEditorContent.pasteHtml(html);
                    }
                }

                radEditorContent.showExternalDialog(
                '' + '/Admin/Modules/FileManager/InsertVideo.aspx', {},
                800, 620, insertVideoCallbackFunction, null, null, true,
                Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move, false, true);
            };

            // Insert Custom Audio
            Telerik.Web.UI.Editor.CommandList["CustomAudioManager"] = function (commandName, radEditorContent, args) {
                var insertAudioCallbackFunction = function (sender, args) {
                    if (!args.IsCancel) {
                        var html = GetAudioAttribute(args);
                        radEditorContent.pasteHtml(html);
                    }
                }

                radEditorContent.showExternalDialog(
                '' + '/Admin/Modules/FileManager/InsertAudio.aspx',
                {},
                800,
                620,
                insertAudioCallbackFunction,
                null,
                null,
                true,
                Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
                false,
                true);

            };

           
                    

        </script>
    </div>
    </form>
</body>
</html>
